﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace octalforty.Componento.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "ComponentsFeed",
                "components/feed",
                new { controller = "Feed", action = "ComponentsFeed" });

            routes.MapRoute(
                "BrowseComponents",
                "components/browse",
                new { controller = "Browse", action = "Index" });

            routes.MapRoute(
                "PublishComponent",
                "components/publish",
                new { controller = "Publish", action = "Index" });
            
            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults

            );

        }

        protected void Application_Start()
        {
            //BeginRequest += MvcApplication_BeginRequest;

            RegisterRoutes(RouteTable.Routes);
        }

        private void MvcApplication_BeginRequest(object sender, EventArgs e)
        {
            /*using(var dataContext = new ComponentoDataContext())
            {
                var profile = (from p in dataContext.Profiles
                               where p.OpenID == User.Identity.Name
                               select p).FirstOrDefault();

                if(profile != null)
                    Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(profile.Name), null);
            } // using
             * */
        }
    }
}